/**
 *
 */
package com.biotecan.genome.cytobandviewer.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author SuFeng
 *
 */
public class ChromosomeBuilder {

    //private  static ChromosomeBuilder builder = new ChromosomeBuilder();
    private  Map<Integer, Chromosome> map = new TreeMap<Integer, Chromosome>();
    private List<Cytoband> bands = new ArrayList<Cytoband>();
    private List<Mutation> mutations = new ArrayList<>();

    public  ChromosomeBuilder newBuilder() {
        if (map.isEmpty()) {
            for (int i = 1; i <= 24; i++) {
                Chromosome chr = new Chromosome();
                chr.setChrIndex(i);
                chr.setChrName(Chromosome.convertChromosomeIndexToName(i));
                map.put(chr.getChrIndex(), chr);
            }
        }
        return this;
    }

    /**
     * loader cytoband from a given file
     *
     * @param file
     * @return
     * @throws IOException
     */
    public ChromosomeBuilder withCytoband(File file) throws IOException {

        if (!this.bands.isEmpty()) {
            this.bands.clear();
        }

        try (BufferedReader br = new BufferedReader(new FileReader(file));) {
            String line;
            String[] temp;
            Cytoband band;
            while ((line = br.readLine()) != null) {
                temp = line.split("\t", -1);
                band = new Cytoband(temp[0], Long.parseLong(temp[1]),
                        Long.parseLong(temp[2]), temp[3], temp[4]);
                this.bands.add(band);
            }
            //System.out.println("total Cytoband is  "+ bands.size());
        }
        return this;
    }

    public ChromosomeBuilder withMutations(File file){
        mutations.clear();
        try(BufferedReader reader = Files.newBufferedReader(file.toPath(), Charset.forName("utf-8"))){
            String line;
            String[] array;
            Mutation mutation;
            while((line=reader.readLine())!=null){
                array = line.split("\t", -1);
                mutation = new Mutation(array[0], array[1], array[2], array[3]);
                mutations.add(mutation);
            }
            
        } catch (IOException ex) {
            Logger.getLogger(ChromosomeBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this;
    }
    
    /**
     * create a Chromosome collection object
     *
     * @return
     */
    public Chromosomes builder() {


        //add all cytobands into chromosome map
        for (Cytoband band : bands) {
            //System.out.println("add bands to chromosomes "+(i++));
            map.get(Chromosome.convertChromosomeNameToIndex(band.getChrName()))
                    .getBands().add(band);
        }
        
        //add all muations into chromosome map
        for(Mutation mutation : mutations){
            if(map.get(mutation.getChromosome())!=null){
                map.get(mutation.getChromosome()).addMutation(mutation);
            }
        }
        
        return new Chromosomes(map.values());
    }

    /**
     * @return the bands
     */
    public List<Cytoband> getBands() {
        return bands;
    }

    /**
     * @param bands the bands to set
     */
    public void setBands(List<Cytoband> bands) {
        this.bands = bands;
    }
}
